package com.tommy.ddp.box
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.definition.AxisConfig;
	import com.tommy.ddp.definition.BoxColorType;
	import com.tommy.ddp.definition.BoxExeType;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.grid.DdpGrid;
	import com.tommy.ddp.manager.AssetsManager;
	import com.tommy.ddp.manager.SwapManager;
	import com.tommy.ddp.road.RoadFinder;
	import com.tommy.ddp.road.RoadWalker;
	
	import flash.geom.Point;
	
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.text.TextField;

	/**
	 * 对对碰的方块
	 * @author tommy
	 * 
	 */	
	public class DdpBox extends Sprite
	{
		private var _row:int;
		private var _column:int;
		private var _exeType:uint;//功能类型
		private var _colorType:uint;//颜色类型
		
		//private var btn:Button;
		private var isDown:Boolean
		
		private var orgPoint:Point;
		
		private var targetRow:int;
		private var targetY:int;
		/**
		 * 是否在消除队列中，是就不会被抓出来
		 */		
		public var isInDisposeList:Boolean;
		//
		private var finder:RoadFinder;
		private var walker:RoadWalker;
		private var img:Image;
		
		public function DdpBox()
		{
			finder = new RoadFinder();
			walker = new RoadWalker();
			//walker.addEventListener(RoadWalker.FINISH,onWalkFinish);
			
			img = new Image(AssetsManager.ballTexture);
			addChild(img);
		}
		
		/*private function onWalkFinish(event:Event):void
		{
			trace("walk finish");
		}*/
		/**
		 * 
		 * @param n
		 * 
		 */		
		public function setType(colorType:uint,exeType:uint = BoxExeType.NONE):void
		{
			_colorType = colorType;
			_exeType = exeType;
			//
			/*var s:flash.display.Sprite = new flash.display.Sprite();
			s.graphics.beginFill(color);
			s.graphics.drawCircle(5,5,DDPConfig.GRID_SIZE_H/2 -10/2);
			s.graphics.endFill();*/
			if(_exeType == BoxExeType.BOOM)
				img.texture = AssetsManager.ball_B_Texture;
			else if(_exeType == BoxExeType.SUPER_BOOM)
				img.texture = AssetsManager.ball_SB_Texture;
			else if(_exeType == BoxExeType.X_AXIS_ALL)
				img.texture = AssetsManager.ball_X_Texture;
			else if(_exeType == BoxExeType.Y_AXIS_ALL)
				img.texture = AssetsManager.ball_Y_Texture;
			else if(_exeType == BoxExeType.BOOM_CLONE)
				img.texture = AssetsManager.ball_BC_Texture;
			else
				img.texture = AssetsManager.ballTexture;
			//
			//
			var color:uint;
			if(_colorType == BoxColorType.NONE)
				color = 0xffffff;
			else if(_colorType == BoxColorType.COLOR_1)
				color = 0xff0000;
			else if(_colorType == BoxColorType.COLOR_2)
				color = 0x00ff00;
			else if(_colorType == BoxColorType.COLOR_3)
				color = 0x0000ff;
			else if(_colorType == BoxColorType.COLOR_4)
				color = 0xffff00;
			else if(_colorType == BoxColorType.COLOR_5)
				color = 0x00ffff;
			else if(_colorType == BoxColorType.COLOR_6)
				color = 0xff00ff;
			//
			img.color = color;
			//
			/*var rect:Rectangle = s.getBounds(s);
			var bmd:BitmapData = new BitmapData(s.width,s.height,true,0);
			var matrix:Matrix = new Matrix(1,0,0,1,-rect.x,-rect.y);
			bmd.draw(s,matrix);*/
			
			//img = new Button(Texture.fromBitmapData(bmd));
			img.addEventListener(TouchEvent.TOUCH,onTouch);
		}
		
		public function get roadFinder():RoadFinder
		{
			return finder;
		}
		public function get roadWalker():RoadWalker
		{
			return walker;
		}
		/**
		 * 寻路
		 * 
		 */		
		public function findRoad():void
		{
			finder.find(column,row);
			/*var txt:TextField = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.text = finalPoint.x + "," + finalPoint.y;
			//
			var bmd:BitmapData = new BitmapData(txt.width,txt.height,true,0);
			bmd.draw(txt);
			if(img)
			{
				img.removeFromParent(true);
			}
			img = new Image(Texture.fromBitmapData(bmd));
			img.touchable = false;
			addChild(img);*/
		}
		/**
		 * 移动
		 * 
		 */		
		public function walk():void
		{
			walker.walk(this,finder.path);
		}
		/**
		 * 该棋子最终会在什么位置
		 * @return 
		 * 
		 */		
		public function get finalPoint():Point
		{
			if(finder.path.length > 0)
			{
				return finder.path[finder.path.length - 1];
			}
			else
				return new Point(column,row);
		}
		
		
		/**
		 * 移动到某个行（下落效果）
		 * @param i
		 * 
		 */		
		public function moveRow(i:int):Boolean
		{
			/*if(i == this.row)
				return false;
			//
			
			//
			targetRow = i;
			targetY = targetRow*DDPConfig.GRID_SIZE_H;
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);*/
			return true;
		}
		
		/*private function onEnterFrame(event:Event):void
		{
			this.y += DDPConfig.MOVE_ROW_SPEED;
			if(this.y >= targetY)
			{
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				row = targetRow;
				GridDataManager.regBox(column,row,this);
				
				//|||FillUpManager.addFinishNum(this);
			}
		}*/
		
		private function onTouch(event:TouchEvent):void
		{
			var touch:Touch = event.getTouch(stage);
			if(!touch)
				return;
			//
			var grid:DdpGrid = GridDataManager.getGrid(column,row);
			if(!grid.isAvaliable)
				return;
			//
			if(touch.phase == TouchPhase.BEGAN)
			{
				//调试
				if(event.ctrlKey)
				{
					var t:uint = Math.floor(Math.random()*6);
					if(t == BoxExeType.BOOM_CLONE)
						t = BoxExeType.NONE;
					setType(_colorType,t);
					return;
				}
				else if(event.shiftKey)
				{
					setType(Math.floor(Math.random()*6),_exeType);
					return;
				}
				//end
				
				isDown = true;
				orgPoint = new Point(touch.globalX,touch.globalY);
			}
			else if(touch.phase == TouchPhase.ENDED)
			{
				isDown = false;
			}
			else if(touch.phase == TouchPhase.MOVED)
			{
				if(isDown)
				{
					checkMovePos(touch);
				}
			}
		}
		
		public function get colorType():uint
		{
			return _colorType;
		}
		
		public function get exeType():uint
		{
			return _exeType;
		}
		
		
		/**
		 * 第几行/Y
		 * @param v
		 * 
		 */		
		public function set row(v:int):void
		{
			_row = v;
			this.y = _row*DDPConfig.GRID_SIZE_H;
		}
		/**
		 * 第几列/X
		 * @param v
		 * 
		 */		
		public function set column(v:int):void
		{
			_column = v;
			this.x = _column*DDPConfig.GRID_SIZE_W;
		}
		
		public function get row():int
		{
			return _row;
		}
		public function get column():int
		{
			return _column;
		}
		
		override public function dispose():void
		{
			/*this.scaleX = 1.5;
			return;*/
			super.dispose();
			
			if(img)
			{
				GridDataManager.disposeGridofBoxPos(this);
				
				GridDataManager.removeBox(this);
				this.removeFromParent();
				img.removeEventListener(TouchEvent.TOUCH,onTouch);
				//
				finder.dispose();
				finder = null;
				
				walker.dispose();
				walker = null;
			}
			img = null;
			orgPoint = null;
		}
		/*private function onTween():void
		{
			if(btn)
			{
				GridDataManager.removeBox(this);
				this.removeFromParent();
				btn.removeEventListener(TouchEvent.TOUCH,onTouch);
			}
			btn = null;
			orgPoint = null;
		}*/
		//
		
		private function checkMovePos(touch:Touch):void
		{
			var axis:int;
			var dir:int;
			
			if(Math.abs(touch.globalX - orgPoint.x) > DDPConfig.GRID_SIZE_W - 10)
			{
				axis = AxisConfig.X_AXIS;
				
				if(touch.globalX < orgPoint.x)
					dir = -1;
				else
					dir = 1;
				//
				isDown = false;
				SwapManager.swapBox(this,axis,dir);
			}
			else if(Math.abs(touch.globalY - orgPoint.y) > DDPConfig.GRID_SIZE_H - 10)
			{
				axis = AxisConfig.Y_AXIS;
				
				if(touch.globalY < orgPoint.y)
					dir = -1;
				else
					dir = 1;
				//
				isDown = false;
				SwapManager.swapBox(this,axis,dir);
			}
		}
		
		/*public function ss():void
		{
			this.scaleY = 1.5;
		}*/
	}
}